﻿using WWLauncher.Constant;
using WWLauncher.Util;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Collections.Generic;
using System.Linq;


namespace WWLauncher.ViewModel
{
    /// <summary>
    /// 程序数据
    /// </summary>
    [Serializable]
    public class MainViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<MenuViewModel> menuList; //菜单信息及菜单对应icon信息
        private AppConfig appConfig; //程序设置信息
        private ObservableCollection<ToDoViewModel> hiToDoList; //历史待办
        private ObservableCollection<ToDoViewModel> toDoList; //未处理待办 为了提高任务效率 分开处理
        private ObservableCollection<IconInfo> selectedMenuIcons;
        private int selectedMenuIndex;  //上次选中菜单索引
        public MainViewModel()
        {
        }
        public MainViewModel(bool startedShowPanel)
        {
            AppConfig.StartedShowPanel = startedShowPanel;
        }
        public ObservableCollection<ToDoViewModel> HiToDoList
        {
            get
            {
                if (hiToDoList == null)
                {
                    hiToDoList = new ObservableCollection<ToDoViewModel>();

                }
                return hiToDoList;
            }
            set
            {
                hiToDoList = value;
                OnPropertyChanged("HiToDoList");
            }
        }

        public ObservableCollection<ToDoViewModel> ToDoList
        {
            get
            {
                if (toDoList == null)
                {
                    toDoList = new ObservableCollection<ToDoViewModel>();
                }
                return toDoList;
            }
            set
            {
                toDoList = value;
                OnPropertyChanged("ToDoList");
            }
        }

        public ObservableCollection<MenuViewModel> MenuList
        {
            get
            {
                if (menuList == null)
                {
                    menuList = new ObservableCollection<MenuViewModel>();
                }
                return menuList;
            }
            set
            {
                menuList = value;
                OnPropertyChanged("MenuList");
            }
        }
        public ObservableCollection<IconInfo> SelectedMenuIcons
        {
            get
            {
                return selectedMenuIcons;
            }
            set
            {
                selectedMenuIcons = value;
                OnPropertyChanged("SelectedMenuIcons");
            }
        }
        /// <summary>
        /// 上次选中菜单索引
        /// </summary>
        public int SelectedMenuIndex
        {
            get
            {
                return selectedMenuIndex;
            }
            set
            {
                selectedMenuIndex = value;
                OnPropertyChanged("SelectedMenuIndex");
            }
        }

        public AppConfig AppConfig
        {
            get
            {
                if (appConfig == null)
                {
                    appConfig = new AppConfig();
                }
                return appConfig;
            }
            set
            {
                appConfig = value;
                OnPropertyChanged("AppConfig");
            }
        }
        private static readonly object _MyLock = new object();
        private static MainViewModel _mainViewModel;

        public static MainViewModel Instance()
        {
            lock (_MyLock)
            {
                if (_mainViewModel != null)
                {
                    return _mainViewModel;
                }

                if (_mainViewModel == null && !File.Exists(Constants.DataFilePath))
                {
                    _mainViewModel = new MainViewModel();
                    ConfigUtil.SaveAppData(_mainViewModel, Constants.DataFilePath);
                    return _mainViewModel;
                }
                else
                {
                    _mainViewModel = ConfigUtil.RefreshAppData(new FileInfo(Constants.DataFilePath));

                    if (_mainViewModel == null)
                    {
                        throw new Exception("数据文件是损坏的。");
                    }
                    //将菜单密码写入文件
                    if (!string.IsNullOrEmpty(_mainViewModel.AppConfig.MenuPassword))
                    {
                        ConfigUtil.SavePassword(_mainViewModel.AppConfig.MenuPassword);
                    }
                    return _mainViewModel;
                }
            }
        }


        public List<IconInfo> AllIcons()
        {
            List<IconInfo> list = new List<IconInfo>();

            Instance().MenuList.ToList().ForEach(i =>
            {
                foreach (var item in i.IconList)
                {
                    list.Add(item);
                }
            });
            return list;
        }
        [field: NonSerialized()]
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
